from collections import OrderedDict

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


def jwt_response_payload_handler(token, user=None, request=None):
    """
    Returns the response data for both the login and refresh views.
    Override to return a custom response such as including the
    serialized representation of the User.

    Example:

    def jwt_response_payload_handler(token, user=None, request=None):
        return {
            'token': token,
            'user': UserSerializer(user, context={'request': request}).data
        }

    """
    return {
        'token': token,
        "username": user.username,
        'id': user.id
    }


# 自定义分页类
class PageNum(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'pagesize'
    max_page_size = 20 # 每页返回的最大值


    def get_paginated_response(self, data):

        return Response(OrderedDict([
            ('pages', self.page.paginator.num_pages), # 总页数
            ('page', self.page.number), # 第几页
            ('lists', data)

        ]))